#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

typedef struct _tlv_t_ {
    short type;
    short length;
    char data[];
} Message;

int main(int argc, char* argv[])
{
    int pid = atoi(argv[1]);
    int sig = atoi(argv[2]);
    char* data = argv[3];
    int len = (sizeof(Message) + strlen(data) + 1);
    int size = ((len / 4) + !!(len % 4)) * 4; // 4字节对齐
    Message* msg = malloc(size);

    if (msg) {
        union sigval sv = {0};
        int i = 0;
        int* pi = (int*)msg;
        
        printf("current pid(%d) ...\n", getpid());
        printf("send sig(%d) to process(%d)...\n", sig, pid);
        
        msg->type = 0;
        msg->length = len - sizeof(Message);

        strcpy(msg->data, data);

        for (i = 0; i < size; i+=4) {
            sv.sival_int = *pi++;

            sigqueue(pid, sig, sv); // 一次发送4个字节
        }
    }

    free(msg);

    return 0;
}
